﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AugustSql
{
 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class ColumAttribute : Attribute
    {
        public string Name { get; set; }
        public SqlValueType Type { get; set; }
        public bool IsPrimaryKey { get; set; }
        private bool _allowNull = true;
        public bool AllowNull
        {
            get => _allowNull;
            set => _allowNull = value;
        }
        private bool _hasDefaultValue = false;
        public bool HasDefaultValue
        {
            get => _hasDefaultValue;
            set => _hasDefaultValue = value;
        }
        public bool AutoIncrement { get; set; }
        private object _defaultValue;

        public object DefaultValue
        {
            get => _defaultValue;
            set
            {
                _defaultValue = value;
                if (_defaultValue != null)
                {
                    HasDefaultValue = true;
                }
            }
        }

        /// <summary>
        /// 索引键
        /// </summary>
        public bool IsIndexKey { get; set; }
        public bool SetLength { get; internal set; }
        public int Length { get; set; }
        public bool IsSerchKey { get; set; }
        public bool CheckNull { get; set; }
        /// <summary>
        /// 不导出CSV列
        /// </summary>
        public bool NoCsvColum { get; set; }
        public object CsvUnit { get; set; }

        public string CreatInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"`{Name}` ");
            if (Type == SqlValueType.None)
                throw new Exception("类型需要设置仅仅支持：BIGINT/long INT/int FLOAT/float VARCHAR/string DATETIME/DateTime");
            sb.Append($"{Type} ");
            sb.Append(SetLength ? $"({Length}) " : "");
            sb.Append(AllowNull ? "NULL " : "NOT NULL ");
            if (HasDefaultValue)
            {
                if (AutoIncrement)
                    sb.Append("AUTO_INCREMENT ");
                else
                    if (DefaultValue != null)
                    sb.Append($"DEFAULT '{DefaultValue}' ");
                else
                    sb.Append($"DEFAULT NULL ");
            }
            sb.Append(",");
            return sb.ToString();
        }
    }

}
